package com.shipwe.loans.utils.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.shipwe.adapter.ShipweOnItemChildClickListener
import com.shipwe.adapter.ShipweRecyclerViewAdapter
import com.shipwe.adapter.ShipweViewHolderHelper
import com.shipwe.loans.R
import com.shipwe.loans.databinding.DialogAuthWalletSubmitBinding
import com.shipwe.loans.pro.ShipweDialog
import com.shipwe.loans.utils.AppDensityUtil
import com.shipwe.loans.utils.AppLoadImageUtil
import com.shipwe.loans.utils.decoration.LinearSpacingItemDecoration
import com.shipwe.loans.utils.event.AppEventPointHelper
import com.shipwe.loans.utils.event.AppEventPointType
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/21 13:45
 * @Annotation：绑卡确认dialog
 */
class AuthWalletSubmitDialog: ShipweDialog {
    private lateinit var binding: DialogAuthWalletSubmitBinding
    private var mCallback: IDialogCallback? = null

    private var itemAdapter: ItemAdapter? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogAuthWalletSubmitBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        initWalletListView()
    }

    override fun initListener() {
        setOnClickListener(binding.walletSubmit)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    /**
     * 初始化账户列表
     */
    private fun initWalletListView() {
        val topDecoration = AppDensityUtil.dp2px(12f)
        binding.walletListView.addItemDecoration(LinearSpacingItemDecoration(0, 0, topDecoration, topDecoration))
        binding.walletListView.layoutManager = LinearLayoutManager(context)
        itemAdapter = ItemAdapter(binding.walletListView)
        binding.walletListView.adapter = itemAdapter!!
        binding.walletListView.itemAnimator = DefaultItemAnimator()
        itemAdapter!!.setOnItemChildClickListener(ShipweOnItemChildClickListener { _, childView, position ->
        })
    }

    /**
     * 设置账户列表
     */
    fun setBindAccountList(accountList: MutableList<JSONObject>){
        itemAdapter!!.data = accountList
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    class ItemAdapter(recyclerView: RecyclerView?) : ShipweRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.fragment_step_auth_wallet_item) {
        override fun setItemChildListener(helper: ShipweViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }
        override fun fillData(helper: ShipweViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.walletTitle,model.optString("walletDesc"))
            helper.setText(R.id.walletMobile,"+" + model.optString("walletMobile"))
            val walletLogo = helper.getImageView(R.id.walletLogo)
            AppLoadImageUtil.instance.loadImageUrl(mContext,walletLogo,model.optString("walletLogo"), R.drawable.load_holder)

            helper.setVisibility(R.id.walletDelete,View.INVISIBLE)
        }
    }

    override fun onStart() {
        super.onStart()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_DISMISS)
    }
}